Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| can_compute_loss = True if len(self.label_names) == 0 and self.can_return_loss else False |
There was a problem hiding this comment.
We need to restrict to len(self.label_names) == 0.
For models that has len(self.label_names) > 0, we should check if the inputs contain the required labels by the model - which is done one line above for has_labels.
There was a problem hiding this comment.
can_compute_loss actually means can_compute_loss_without_labels, but maybe a too long name.
There was a problem hiding this comment.
Can we rename all can_compute_loss to loss_without_labels then? It's more informative even if not completely perfect.
sgugger
left a comment
There was a problem hiding this comment.
Very nice idea thanks a lot! I left a few comments regarding naming and we can make the function can_return_loss a bit better, but overall great PR!
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| can_compute_loss = True if len(self.label_names) == 0 and self.can_return_loss else False |
There was a problem hiding this comment.
Can we rename all can_compute_loss to loss_without_labels then? It's more informative even if not completely perfect.
src/transformers/utils/generic.py
Outdated
| signature = inspect.signature(model_class.__call__) | ||
| else: | ||
| signature = inspect.signature(model_class.forward) | ||
| return [p for p in signature.parameters if p == "return_loss"] |
There was a problem hiding this comment.
I'd prefer if this returns a bool then rely on Python magic conversion to bools.
Also I think we should check if the default is True as the Trainer won't change the default.
There was a problem hiding this comment.
I changed it to
any(p == "return_loss" for p in signature.parameters)but do you mean we should have sth. like (conceptually)
any(p == "return_loss" and default_value(p) is True for p in signature.parameters)
?
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| loss_without_labels = True if len(self.label_names) == 0 and self.can_return_loss and inputs.get("return_loss", None) is True else False |
There was a problem hiding this comment.
inputs will not contain return_loss if True is the default.
There was a problem hiding this comment.
OK! I see all of our return_loss have None as default value (include CLIP), but I can add extra check to be sure
There was a problem hiding this comment.
Oh in this case then your solution works.
| # If `return_loss` is not specified or being `None` in `inputs`, we check if the default value of `return_loss` | ||
| # is `True` in `model.forward`. | ||
| return_loss = inputs.get("return_loss", None) | ||
| if return_loss is None: |
There was a problem hiding this comment.
If False, no need to check default value
|
|
||
| for p in signature.parameters: | ||
| if p == "return_loss" and signature.parameters[p].default is True: | ||
| return True |
There was a problem hiding this comment.
Will return True only if the default value is True.
|
@sgugger Hopefully the change covers everything that could happen now and in the future. |
…20214) * Allow trainer to return loss for CLIP-like models * Apply suggestions * update * update * update Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
What does this PR do?
Allow trainer to give evaluation loss for CLIP-like models.
Currently, this line
transformers/src/transformers/trainer.py
Line 3192 in 07d8d6e
gives
has_labels = Falsefor CLIP-like models, and can't give loss value in the evaluation.without this PR:
with this PR.